<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\helper\Time;
use think\Db;
class Index extends Base
{
    public function index()
    {
        $id = session('uid');
        $info = Db::name('auth_group_access')
            ->alias('ac')
            ->join('admin ad ','ac.uid = ad.id','LEFT')
            ->join('auth_group g ','ac.group_id = g.id','LEFT')
            ->where(array('ac.uid'=>$id))
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function main()
    {
        $info = array(
            'web_server' => $_SERVER['SERVER_SOFTWARE'],
            'onload'     => ini_get('upload_max_filesize'),
            'think_v'    => THINK_VERSION,
            'phpversion' => phpversion(),
            'caozuoxitong' =>PHP_OS,
        );
        //当前登录角色
        $id = session('uid');
        $res = Db::name('auth_group_access')
            ->alias('ac')
            ->join('admin ad ','ac.uid = ad.id','LEFT')
            ->join('auth_group g ','ac.group_id = g.id','LEFT')
            ->where(array('ac.uid'=>$id))
            ->find();
        //最新登录8条信息
        $data['login_list'] = $this->loginLogList();
        //待审核文章
        $shenArt  = Db::name('article')->where(array('status'=>0))->count();
        //待上架商品
        $shenGoods  = Db::name('goods')->where(array('is_on_sale'=>0))->count();
        //会员总数
        $shenMember  = Db::name('member')->count();
        //统计表
        $tj = $this->datas($day='6');

        $this->assign('shenArt',$shenArt);
        $this->assign('shenGoods',$shenGoods);
        $this->assign('shenMember',$shenMember);
        $this->assign('tj',$tj);
        $this->assign('data', $data);
        $this->assign('info',$info);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /**
     * @Title: loginLogList
     * @Description: 最新登录8条信息
     * @author 极客开发
     */
    private function loginLogList()
    {
        $loginLogModel = new \app\common\model\LoginLog;
        return $loginLogList = $loginLogModel->limit(6)->order('id DESC')->select();
    }
    //7天数据 从0开始
    public function datas($day='6'){
        $time=Time::dayToNow($day);
        $data=[];
        for($i=0;$i<=$day;$i++){
            $start=$time[0]+(60*60*24*$i);
            $end=$start+60*60*24;
            $map=array(
                'pay_time'=>array('between',[$start,$end]),
            );
            $num=Db::name('order')->where($map)->count();
            $data['num'][$i]=$num;
            $data['date'][$i]=date('m-d',$start);
        }
        $data = json_encode($data);
        return $data;



    }
}